package weaver.interfaces.gkgk.duss.action;

import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.gkgk.duss.utils.GkgkUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;

/**
 * @Title: ecology-9-demo
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022/03/08
 * @Description: 付款申请/预付款申请：相关合同会选择多个（合同台账浏览按钮），可能会同时选择主合同和补充合同，
 * * 如果选择的里面有补充合同，那么就需要把付款金额累计到主合同内.
 * @ModifyLog:
 **/

public class WeaFksqlcAction implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        RecordSet rs = new RecordSet();
        BaseBean baseBean = new BaseBean();
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        baseBean.writeLog(">>>>>WeaFksqlcAction>>>>>requestid=" + requestid);
        GkgkUtil gkgkUtil = new GkgkUtil();
        try {
            rs.executeQuery("select id,zfkjey,xght from " + tablename + " where requestid=?", requestid);
            rs.next();
            String zfkjey = Util.null2String(rs.getString("zfkjey"));
            String xght = Util.null2String(rs.getString("xght"));
            String zhtid = Util.null2String(gkgkUtil.getHtid(xght).get("zhtid"));
            String bchtid = Util.null2String(gkgkUtil.getHtid(xght).get("bchtid"));
            baseBean.writeLog(">>>>>WeaFksqlcAction>>>>>zhtid="+zhtid);
            baseBean.writeLog(">>>>>WeaFksqlcAction>>>>>bchtid="+bchtid);
            String[] zhtArray = zhtid.split(",");
            String[] bchtArray = bchtid.split(",");
            if (zhtArray.length <= 1) {
                //只有主合同
                if (zhtArray.length == 1 && "".equals(bchtid)) {
                    for (String htid : zhtArray) {
                        rs.executeUpdate("update uf_httz set fkje=fkje+" + zfkjey + " where id=?", htid);
                        baseBean.writeLog(">>>>>WeaFksqlcAction>>>>>主合同=sql:" + "update uf_httz set fkje=fkje+" + zfkjey + " where id=" + htid);
                    }
                }

                //只有补充合同/既有主合同，又有补充合同
                if (zhtArray.length <= 1 && !"".equals(bchtid)) {
                    for (String htid : bchtArray) {
                        rs.executeUpdate("update uf_httz set fkje=fkje+" + zfkjey + " where id=?", htid);
                        baseBean.writeLog(">>>>>WeaFksqlcAction>>>>>补充合同=sql:" + "update uf_httz set fkje=fkje+" + zfkjey + " where id=" + htid);
                    }
                }
                return Action.SUCCESS;
            } else {
                requestInfo.getRequestManager().setMessageid("111111");
                requestInfo.getRequestManager().setMessagecontent("Action接口异常：主合同数量不正确");
                return Action.FAILURE_AND_CONTINUE;
            }
        } catch (Exception e) {
            e.printStackTrace();
            baseBean.writeLog("异常参考:" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口异常！");
            return Action.FAILURE_AND_CONTINUE;
        }
    }
}
